package org.flower.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.flower.bean.CommodityDetails;
import org.flower.bean.Order;
import org.flower.dao.OrderDao;
import org.flower.dao.impl.OrderDaoImpl;
import org.flower.mapper.OrderMapper;
import org.flower.service.OrderService;
import org.flower.utils.MyBatisUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 订单服务impl
 *
 * @author Aizen
 * @date 2021/12/24
 */
public class OrderServiceImpl implements OrderService {
    @Override
    public List<Order> getById(int id,String zt) {
        OrderDao orderDao = new OrderDaoImpl();
        List<Order> o = orderDao.getById(id,zt);
        return o;
    }

    @Override
    public int getNum(int id,int zt) {
        OrderDao orderDao = new OrderDaoImpl();
        int num = orderDao.getNum(id,zt);
        return num;
    }

    @Override
    public PageInfo<Order> findAllOrder(Order order,int startRow) {
        SqlSession sqlSessionFactory = null;
        try {
            sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        } catch (IOException e) {
            e.printStackTrace();
        }
        OrderMapper mapper = sqlSessionFactory.getMapper(OrderMapper.class);
        PageHelper.startPage(startRow,10);
        List<Order> orderList = mapper.getAllOrder(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList, 10);
        List<Order> list = orderPageInfo.getList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (Order o:list) {
            String serviceDateStr = sdf.format(o.getServicedate());
            o.setServicedateStr(serviceDateStr);
            String orderDateStr = sdf.format(o.getOrderdate());
            o.setOrderdateStr(orderDateStr);
        }
        orderPageInfo.setList(list);
        sqlSessionFactory.close();
        return orderPageInfo;
    }

    @Override
    public Order findOrderDetailById(String id) {
        try {
            SqlSession sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
            OrderMapper mapper = sqlSessionFactory.getMapper(OrderMapper.class);
            //查询到该订单对象
            Order order = mapper.findOrderById(id);

            //处理日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            String orderDateStr = sdf.format(order.getOrderdate());
            String serviceDateStr = sdf1.format(order.getServicedate());
            order.setOrderdateStr(orderDateStr);
            order.setServicedateStr(serviceDateStr);


            //封装该订单的详情信息
            List<CommodityDetails> commodityDetailsList = mapper.findOrderDetailById(order.getId());
            //处理商品图片
            commodityDetailsList.forEach(commodityDetails -> {
                String comimage = commodityDetails.getComimage();
                String[] split = comimage.split(",");
                commodityDetails.setComimage(split[0]);
                System.out.println(commodityDetails.getComimage());
            });




            order.setCommodityDetailsList(commodityDetailsList);
            sqlSessionFactory.close();
            return order;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean updateOrder(Order order) {
        OrderDao od = new OrderDaoImpl();
        return od.updateOrder(order);
    }

    @Override
    public List<Order> getAllById(int id) {
        OrderDao orderDao = new OrderDaoImpl();
        List<Order> allById = orderDao.getAllById(id);
        return allById;
    }

    @Override
    public boolean DeleteDing(String id) {
        OrderDao orderDao = new OrderDaoImpl();
        return orderDao.DeleteDing(id);
    }

    @Override
    public boolean updateDing(String id) {
        OrderDao orderDao = new OrderDaoImpl();
        return orderDao.updateDing(id);
    }

    @Override
    public boolean updateDingdan(String id) {
        OrderDao orderDao = new OrderDaoImpl();
        return orderDao.updateOrderStaById(id);
    }


}
